package com.example.threadpool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;

/**
 * 统计任务执行平均时间
 * 1.自定义线程池实现ThreadPoolExecutor
 */
public class TimingThreadPool extends ThreadPoolExecutor {

    // 开始时间
    private final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
    private final Logger log = Logger.getAnonymousLogger();
    // 统计任务数
    private final AtomicLong numTasks = new AtomicLong();
    // 线程池运行总时间
    private final AtomicLong totalTime = new AtomicLong();

    public TimingThreadPool(int corePoolSize,
                            int maximumPoolSize,
                            long keepAliveTime,
                            TimeUnit unit,
                            BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }


    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        log.fine(String.format("Thread %s: start %s", t, r));
        startTime.set(System.nanoTime());
    }


    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        try {
            long endTime = System.nanoTime();
            long taskTime = endTime - startTime.get();
            // 添加调用次数
            numTasks.incrementAndGet();
            // 添加时间
            totalTime.addAndGet(taskTime);
            log.fine(String.format("Thread %s: end %s, time=%dns", t, r, taskTime));
        } finally {
            super.afterExecute(r, t);
        }

    }


    @Override
    protected void terminated() {
        try {
            // 任务执行平均时间
            log.info(String.format("Terminated: average time=%dns", totalTime.get() / numTasks.get()));
        } finally {
            super.terminated();
        }
    }

}
